package org.tech4j.common.core.sign.provider;

import org.tech4j.common.core.sign.api.SignParameters;
import org.tech4j.common.utils.SignUtils;

import java.net.URI;
import java.util.Objects;

/**
 * @program: tech4j-common
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/2 21:26
 **/
public class VersionTwoSignProvider implements SignProvider {

    @Override
    public String generateSign(String signKey, SignParameters signParameters) {
        String data = signParameters.getParameters()
                + getRelativeURL(signParameters.getUri());
        return SignUtils.sign(signParameters.getSignAlg(), signKey, data).toUpperCase();
    }

    private String getRelativeURL(final URI uri) {
        if (Objects.isNull(uri.getQuery())) {
            return uri.getPath();
        }
        return uri.getPath() + "?" + uri.getQuery();
    }
}
